﻿using Nancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Configuration;

namespace customer.dao
{
    public class DBModule : NancyModule
    {   

        public DBModule()
        {

            this.Post("/dblist", x => DBLIST(x));
            this.Get("/dblist", x => DBLIST(x));
        }

        dynamic DBLIST(dynamic x)
        {
            string dblist = ConfigurationManager.AppSettings["DBList"];

            Dictionary<string, string> dic = new Dictionary<string, string>();

            string[] kvs = dblist.Split(",");
            foreach (string kv in kvs)
            {
                if (kv.Contains("[") == false)
                    continue;
                string[] item = kv.Split("[");
                if (item.Length != 2)
                    continue;

                if (dic.ContainsKey(item[0]) == false)
                {   
                    dic.Add(item[0], item[1].Replace("]", ""));      
                }
            }

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(dic);


            return this.Response.AsText(json,"application/json");
        }


    }
}
